package org.dromara.third.convert.riskObject.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.RiskObject;
import org.dromara.system.mapper.RiskObjectMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.riskObject.dto.RiskObjectDTO;
import org.dromara.third.convert.riskObject.validator.RiskObjectValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertRiskObjectController {

    @Autowired
    private RiskObjectMapper riskObjectMapper;

    @PostMapping("/riskObject")
    @Transactional
    public R<Void> addRiskObject(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        RiskObjectDTO riskObjectDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            riskObjectDTO = JSONObject.parseObject(decrypt, RiskObjectDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, riskObjectDTO, new RiskObjectValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        RiskObject riskObject = assembleRiskObjectData(orgCode, riskObjectDTO);
        int count = riskObjectMapper.countWithOutDelFlag(riskObject.getId());
        if (count == 0) {
            riskObjectMapper.insert(riskObject);
        } else {
            riskObjectMapper.updateWithOutDelFlag(riskObject);
        }
        return R.ok();
    }

    private RiskObject assembleRiskObjectData(String orgCode, RiskObjectDTO dto) {
        RiskObject riskObject = new RiskObject();
        try {
            riskObject.setId(dto.getId());
            riskObject.setRiskObjectCode(dto.getRiskObjectCode());
            riskObject.setAnalysisName(dto.getAnalysisName());
            riskObject.setRiskObjectType(dto.getRiskObjectType());
            riskObject.setIsDanger(dto.getIsDanger());
            if (Objects.equals("1", riskObject.getIsDanger())) {
                riskObject.setHazardCode(dto.getHazardCode());
            }
            riskObject.setHazardDept(dto.getHazardDept());
            riskObject.setHazardLiablePerson(dto.getHazardLiablePerson());
            riskObject.setAreaSite(dto.getAreaSite());
            riskObject.setDelFlag(Long.parseLong(dto.getDelFlag()));
            riskObject.setCreateDept(Long.parseLong(dto.getCreateDept()));
            riskObject.setCreateBy(Long.parseLong(dto.getCreateBy()));
            riskObject.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            riskObject.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            riskObject.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            riskObject.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装风险分析对象信息异常");
        }
        return riskObject;
    }
}
